﻿using JESAI.Application.Abstractions.Dtos;
using JESAI.Application.Abstractions.DynamicApi.Dtos;
using JESAI.Application.Abstractions.Sys;
using JESAI.Application.Abstractions.Sys.Dtos;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Core;
using JESAI.Core.Attributes;
using JESAI.Core.Extensions;
using JESAI.Platform.Domain.DynamicApi.Entities;
using JESAI.Platform.Domain.Sys.Entities;
using JESAI.Platform.Domain.Sys.Repositories;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.Sys
{
    [AppService]
    public class DynamicDataSourceService : IDynamicDataSourceService
    {
        private readonly IDynamicDataSourceRepository _dynamicDataSourceRepository;
        private readonly IUser _user;

        public DynamicDataSourceService(IDynamicDataSourceRepository dynamicDataSourceRepository, IUser user)
        {
            _dynamicDataSourceRepository = dynamicDataSourceRepository;
            _user = user;
        }

        public async Task DeleteDynamicDataSource(string id)
        {
            await _dynamicDataSourceRepository.DeleteByIdAsync(id);
        }

        public async Task DeleteDynamicDataSources(string[] ids)
        {
            await _dynamicDataSourceRepository.DeleteByIdsAsync(ids);
        }

        public async Task<DynamicDataSourceDto> GetDynamicDataSource(string id)
        {
            var model = await _dynamicDataSourceRepository._DbQueryable.Where(x=>x.Id==id).FirstAsync();
            return model.MapTo<DynamicDataSourceDto>();
        }

        public async Task<List<DynamicDataSourceDto>> GetDynamicDataSources(string[] ids)
        {
            var models = await _dynamicDataSourceRepository._DbQueryable.Where(it => ids.Contains(it.Id)).ToListAsync();
            return models.MapTo<List<DynamicDataSourceDto>>();
        }

        public async Task<BasePageOutput<List<DynamicDataSourceDto>>> QueryPageDynamicDataSources(QueryDynamicDataSourcePageParam param)
        {
            var totalNumber = 0;
            var modelList = _dynamicDataSourceRepository._DbQueryable
                               .WhereIF(param.IsValid.HasValue, it => it.IsValid == param.IsValid)
                               .WhereIF(!param.EName.IsEmpty(), it => it.EName == param.EName)
                               .WhereIF(param.CreateTime.HasValue, it => it.CreateTime == param.CreateTime)
                               .WhereIF(param.UpdateTime.HasValue, it => it.UpdateTime == param.UpdateTime)
                               .WhereIF(param.DbType.HasValue, it => it.DbType == (DbType)param.DbType.Value)
                                .WhereIF(!param.Description.IsEmpty(), it => it.Description == param.Description)
                                 .WhereIF(!param.CName.IsEmpty(), it => it.CName == param.CName)
                               .ToPageList(param.PageIndex, param.PageSize, ref totalNumber);

            return new BasePageOutput<List<DynamicDataSourceDto>> { Data = modelList.MapTo<List<DynamicDataSourceDto>>(), CurrentPage = param.PageIndex, Total = totalNumber };
        }

        public async Task SetDynamicDataSource(DynamicDataSourceInput input)
        {
            var model = input.MapTo<DynamicDataSourceEntity>();
            if (model.Id.IsNullOrEmpty())
            {
                model.Id = Guid.NewGuid().ToString();
                model.CreateTime = DateTime.Now;
                model.Cid = _user.Id.ToLong();
                await _dynamicDataSourceRepository.InsertAsync(model);
            }
            else
            {
                model.UpdateTime = DateTime.Now;
                model.Uid = _user.Id.ToLong();
                await _dynamicDataSourceRepository.UpdateAsync(model);
            }
        }
    }
}
